package com.sinochem.yunlian.ship.common.util;

import com.sinochem.yunlian.ship.common.constant.BizCode;
import com.sinochem.yunlian.ship.common.constant.RedisKey;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @Title: id生成器
 * @Description: 用于生成业务相关序列号，ID等
 */
public class IdGenerator {

	private RedisTemplate<String,Object> redisTemplate;

	public IdGenerator(RedisTemplate redisTemplate){
		this.redisTemplate = redisTemplate;
	}

	public void setRedisTemplate(RedisTemplate redisTemplate){
		this.redisTemplate = redisTemplate;
	}

	@SuppressWarnings({ "unchecked", "deprecation" })
	public long generatIdByIncr(String key, int expire){
		key= RedisKey.KEY_BIZ_NO + key;
		long res = redisTemplate.opsForValue().increment(key,1L);
		redisTemplate.expire(key,expire, TimeUnit.SECONDS);
		return res;
	}

	/**
	 * @param prefix 业务标识
	 * @param bizNo 业务编号
	 * */
	@SuppressWarnings({ "unchecked", "deprecation" })
	public String generatorSerialNo(String prefix ,String bizNo){
		String dateTimeStr = DateUtils.getDate("yyMMddHH");
		String dateStr = dateTimeStr.substring(0,6);
		String timeStr = dateTimeStr.substring(6);
		String redisKey = prefix + bizNo + dateStr;
		long seriaNo = generatIdByIncr(redisKey,24*60*60);
		StringBuffer buffer = new StringBuffer();
		String seriaNumber = buffer.append(prefix).append(dateStr).append(String.format("%05d", seriaNo)).append(timeStr).toString();
		return seriaNumber;
	}

	/**
	 * 货盘编号
	 * */
	public String generatorMaterialNo() {
		return  generatorSerialNo(BizCode.BIZ_HP_PREFIX,BizCode.BIZ_HP);
	}

	/**
	 * 订单编号
	 * */
	public String generatorOrderNo() {
		return  generatorSerialNo(BizCode.BIZ_DD_PREFIX,BizCode.BIZ_DD);
	}

	/**
	 * 获取订单业务编码
	 * @return
	 */
	public String getDDNo(){
		return generatorSerialNo(BizCode.BIZ_DD_PREFIX,BizCode.BIZ_DD);
	}

	/**
	 * 获取运单业务编码
	 * @return
	 */
	public String getYDNo(){
		return generatorSerialNo(BizCode.BIZ_YD_PREFIX,BizCode.BIZ_YD);
	}

	/**
	 * 获取分享业务编码
	 * @return
	 */
	public String getSHNo(){
		return generatorSerialNo(BizCode.BIZ_SH_PREFIX,BizCode.BIZ_SH);
	}

	/**
	 * 船舶编号
	 * */
	public String getShipNo(){
		String key= RedisKey.KEY_BIZ_NO + "ship_no";
		long res = redisTemplate.opsForValue().increment(key,1L);
		String no = String.format("%06d",res);
		return  no;
	}

	/**
	 * 公司编号
	 * */
	public String getCompanyNo(){
		String key= RedisKey.KEY_BIZ_NO + "company_no";
		long res = redisTemplate.opsForValue().increment(key,1L);
		String no = String.format("%08d",res);
		return  no;
	}

	/**
	 * 获取港口编码
	 * @return
	 */
	public String getGKNo(){
		return generatorSerialNo(BizCode.BIZ_GK_PREFIX,BizCode.BIZ_GK);
	}
	
	/**
	 * 获取油品编号
	 * @return
	 */
	public String getFuelNo() {
		String key= RedisKey.KEY_BIZ_NO + "YP";
		long res = redisTemplate.opsForValue().increment(key,1L);
		String no = String.format("%04d",res);
		return  "YP" + no;
	}
	/**
	 * 获取油品订单号
	 * @return
	 */
	public String getYPNo(){
		return newGeneratorSerialNo(BizCode.BIZ_DD_PREFIX,BizCode.BIZ_DD);
	}
	/**
	 * @param prefix 业务标识
	 * @param bizNo 业务编号
	 * */
	public String newGeneratorSerialNo(String prefix ,String bizNo){
		String dateTimeStr = DateUtils.getDate("yyMMddHH");
		String dateStr = dateTimeStr.substring(0,6);
		String timeStr = dateTimeStr.substring(6);
		String redisKey = prefix + bizNo + dateStr;
		long seriaNo = generatIdByIncr(redisKey,24*60*60);
		StringBuffer buffer = new StringBuffer();
		String seriaNumber = buffer.append(prefix).append(bizNo).append(dateStr).append(String.format("%05d", seriaNo)).append(timeStr).toString();
		return seriaNumber;
	}


}
